package com.quktzhuanka.app.my;

import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.quktzhuanka.app.CaiNiaoApplication;
import com.quktzhuanka.app.R;
import com.quktzhuanka.app.activity.BindActivity;
import com.quktzhuanka.app.base.BaseActivity;
import com.quktzhuanka.app.bean.PddClient;
import com.quktzhuanka.app.bean.Response;
import com.quktzhuanka.app.bean.ShopActicleBean;
import com.quktzhuanka.app.common.ACache;
import com.quktzhuanka.app.common.SPUtils;
import com.quktzhuanka.app.config.Constants;
import com.quktzhuanka.app.https.HttpUtils;
import com.quktzhuanka.app.https.onOKJsonHttpResponseHandler;
import com.quktzhuanka.app.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

/**
 * 提现
 * Created by yohn on 2018/7/14.
 */

public class PutForwardActivity extends BaseActivity {
    @BindView(R.id.bg_head)
    LinearLayout bg_head;
    @BindView(R.id.txt_rule)
    TextView txtRule;
    @BindView(R.id.tv_left)
    TextView tv_left;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.edt_money)
    TextView edt_money;
    @BindView(R.id.tv_commit)
    TextView tv_commit;//确认
    @BindView(R.id.et_oldpsd)
    TextInputEditText etOldpsd;
    @BindView(R.id.et_newpsd_sure)
    TextInputEditText etNewpsdSure;
    @BindView(R.id.et_newpsd_sure1)
    TextInputEditText etNewpsdSure1;
    String token, balance;
    private ACache mAcache;

    @Override
    protected void initUI() {
        setContentView(R.layout.ac_put_forward);
        ButterKnife.bind(this);
        getKeTiXian();
        getRule();
    }
    private void getRule(){
        RequestParams shopParams = new RequestParams();
        shopParams.put("article_id", 28);
        HttpUtils.post(Constants.MESSAGE_ARTICLE_GETARTICLEMSG_URL, shopParams, new onOKJsonHttpResponseHandler<ShopActicleBean>(new TypeToken<Response<ShopActicleBean>>() {
        }) {

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Response<ShopActicleBean> datas) {
                if (datas.isSuccess()) {
                    ShopActicleBean.ArticleAsg article_msg = datas.getData().getArticle_msg();
                    if (article_msg != null) {
                        txtRule.setText(Html.fromHtml(article_msg.getContent()));
                    }
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }
    private void getKeTiXian(){
        RequestParams requestParams = new RequestParams();
        HttpUtils.post(Constants.GET_KETIXIAN, requestParams, new TextHttpResponseHandler(){
            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object=new JSONObject(responseString);
                    if(object.getInt("code")==0){
                        edt_money.setText("可提现金额:" +object.getJSONObject("data").getString("amount"));
                    }else {
                        edt_money.setText("可提现金额:" +"0.00");
                        if ("用户不存在".equals(object.getString("msg"))){
                            finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    edt_money.setText("可提现金额:" +"0.00");
                }
            }
        });
    }

    @Override
    protected void initData() {
        mAcache = ACache.get(this);
        token = mAcache.getAsString(Constants.TOKEN);
        Bundle b = getIntent().getExtras();
        if (b != null) {
            if (b.containsKey("balance")) {
                balance = b.getString("balance");
            }
        }
//        if (!TextUtils.isEmpty(balance)) {
//            edt_money.setText("可提现金额:" + df.format((Double.valueOf(balance)-Double.valueOf(SPUtils.getStringData(this,"my_money_three","0.00"))<0?0:(Double.valueOf(balance)-Double.valueOf(SPUtils.getStringData(this,"my_money_three","0.00"))))));
//        } else {
//            edt_money.setText("可提现金额:" +"0.00");
//        }
        tv_left.setVisibility(View.VISIBLE);
        tv_title.setText("余额提现");
        tv_left.setVisibility(View.VISIBLE);
        tv_left.setVisibility(View.VISIBLE);
        etOldpsd.setText(CaiNiaoApplication.getUserInfoBean().user_msg.alipay_account);
        etNewpsdSure.setText(CaiNiaoApplication.getUserInfoBean().user_detail.truename);
    }


    @Override
    protected void initListener() {
        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawRequest();
            }
        });
    }

    private void drawRequest() {
        if (TextUtils.isEmpty(StringUtils.doViewToString(etNewpsdSure1))) {
            showToast("请输入提现金额");
            return;
        }
        if(Double.valueOf(edt_money.getText().toString().split(":")[1])<=Double.valueOf(etNewpsdSure1.getText().toString().trim())){
            showToast("不能大于可提现金额");
            return;
        }
        String time=String.valueOf(System.currentTimeMillis() / 1000);
        RequestParams requestParams = new RequestParams();
        requestParams.put("data_type",PddClient.data_type);
        requestParams.put("version",PddClient.version);
        requestParams.put("timestamp",time);
        requestParams.put("token",SPUtils.getStringData(PutForwardActivity.this,"token",""));
        requestParams.put("type","hkx.userDraw.draw");
        requestParams.put("account_type","1");
        requestParams.put("account", etOldpsd.getText().toString().trim());
        requestParams.put("truename", etNewpsdSure.getText().toString().trim());
        requestParams.put("money",etNewpsdSure1.getText().toString().trim());
        Map<String,String> temp=new HashMap<>();
        temp.put("data_type",PddClient.data_type);
        temp.put("version",PddClient.version);
        temp.put("timestamp",time);
        temp.put("token",SPUtils.getStringData(PutForwardActivity.this,"token",""));
        temp.put("type","hkx.userDraw.draw");
        temp.put("account_type","1");
        temp.put("account", etOldpsd.getText().toString().trim());
        temp.put("truename", etNewpsdSure.getText().toString().trim());
        temp.put("money",etNewpsdSure1.getText().toString().trim());
        String sign=PddClient.getSign1(temp);
        requestParams.put("sign",sign);
        HttpUtils.post(Constants.DRAW, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("fsdfds",responseString);
            }

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("fsdfds",responseString);
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    int code = jsonObject.optInt("code");
                    String msg = jsonObject.optString("msg");
                    if (code == 0) {
                        showToast(msg);
                        finish();
                    } else {
                        showToast(msg);
                        if ("用户不存在".equals(msg)){
                            finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    @OnClick(R.id.get_old_sms)
    public void onViewClicked() {
        openActivity(BindActivity.class);
    }
}
